package com.ecoolex.ying.space.admin.graphql;

import static com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type.Admin;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.service.storage.FileSize;
import com.ecoolex.framework.modularize.api.service.storage.MimeType;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.modularize.api.service.storage.Upload;
import com.ecoolex.framework.modularize.api.service.storage.UploadLimit;
import com.ecoolex.framework.modularize.api.service.storage.UploadSource;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.EstateProjectActivityBrand;
import com.ecoolex.ying.space.pojo.AddEstateProjectActivityBrandDTO;
import com.ecoolex.ying.space.pojo.EditEstateProjectActivityBrandDTO;
import com.ecoolex.ying.space.ws.EstateProjectActivityBrandService;

@GraphQL2Module(type = Admin, description = "地产项目合作品牌管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = EstateProjectActivityBrandGraphql.AUTH, name = "地产项目合作品牌管理")
public class EstateProjectActivityBrandGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-ESTATE-PROJECT-ACTIVITY-BRAND";
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	private static final String AUTH_EDIT = AUTH + "-EDIT";
	
	private static final String AUTH_DEL = AUTH + "-DEL";
	
	private static final String AUTH_ONLINE = AUTH + "-ONLINE";
	
	private static final String AUTH_OFFLINE = AUTH + "-OFFLINE";
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	@Autowired
    private StorageService storageService;
	
	@Reference
	private EstateProjectActivityBrandService service;
	
	@TypeField(type = EstateProjectActivityBrand.class, field = "logo", description = "地产项目合作品牌Logo")
    public StorageInfo image(@Source EstateProjectActivityBrand source) {
        return storageService.get(source.getLogo());
    }
	
	@TypeField(type = EstateProjectActivityBrand.class,field = "canOnline",description = "能上线")
	public Boolean canOnline(@Source EstateProjectActivityBrand source) {
		return Check.eq(false, source.getOnline());
	}
	
	@TypeField(type = EstateProjectActivityBrand.class,field = "canOffline",description = "能下线")
	public Boolean canOffline(@Source EstateProjectActivityBrand source) {
		return Check.eq(true, source.getOnline());
	}
	
	@QueryField(field = "estateProjectActivityBrand" , description = "地产项目合作品牌")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "地产项目合作品牌")
	public EstateProjectActivityBrand get(@NonNull Integer brandId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return service.get(brandId);
	}
	
	@QueryField(field = "estateProjectActivityBrandList" , description = "地产项目合作品牌列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "地产项目合作品牌列表")
	public Pagination<EstateProjectActivityBrand> query(@NonNull Integer projectId,String name,@NonNull Sorting[] sorts,@NonNull Page page,AdminMemberAuthority user) {
		user.assertAuth(AUTH_QUERY);
		return service.query(projectId,name, sorts, page);
	}
	
	@MutationField(field = "uploadEstateProjectActivityBrandLogo", description = { "上传地产项目合作品牌Logo" })
    public StorageInfo uploadEstateProjectActivityBrandLogo(@NonNull Upload request, AdminMemberAuthority user) {
        if (!user.auth(AUTH_ADD) && !user.auth(AUTH_EDIT)) {
            user.notAuthority();
        }
        UploadSource uploadSource = new UploadSource("EstateProjectActivityBrand", "Logo");
        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(10));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());

        return storageService.upload(request, uploadSource, uploadLimit);
    }
	
	@MutationField(field = "addEstateProjectActivityBrand", description = "添加地产项目合作品牌")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加地产项目合作品牌")
	public EstateProjectActivityBrand add(@NonNull AddEstateProjectActivityBrandDTO add,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		
		return service.add(add, user.userName());
	}
	
	@MutationField(field = "editEstateProjectActivityBrand", description = "编辑地产项目合作品牌")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑地产项目合作品牌")
	public EstateProjectActivityBrand edit(@NonNull EditEstateProjectActivityBrandDTO edit,AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		EstateProjectActivityBrand old = service.get(edit.getBrandId());
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "地产项目合作品牌不存在");
		BeanUtil.copy(edit, old);
		return service.edit(old, user.userName());
	}
	
	@MutationField(field = "delEstateProjectActivityBrand", description = "删除地产项目合作品牌")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除地产项目合作品牌")
	public Boolean delete(@NonNull Integer brandId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		EstateProjectActivityBrand old = service.get(brandId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "地产项目合作品牌不存在");
		service.delete(old, user.userName());
		return true;
	}
	
	@MutationField(field = "onlineEstateProjectActivityBrand", description = "上线地产项目合作品牌")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ONLINE, name = "上线地产项目合作品牌")
	public Boolean online(@NonNull Integer brandId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ONLINE);
		EstateProjectActivityBrand old = service.get(brandId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "地产项目合作品牌不存在");
		service.online(old, user.userName());
		return true;
	}
	
	@MutationField(field = "offlineEstateProjectActivityBrand", description = "下线地产项目合作品牌")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_OFFLINE, name = "下线地产项目合作品牌")
	public Boolean offline(@NonNull Integer brandId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_OFFLINE);
		EstateProjectActivityBrand old = service.get(brandId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "地产项目合作品牌不存在");
		service.offline(old, user.userName());
		return true;
	}
}
